package model.classesDAO;

import java.util.List;

import javax.persistence.TypedQuery;

import model.classes.Fabricante;
import model.interfacesDAO.IFabricanteDAO;

public class FabricanteDAO  extends GenericoDAO<Fabricante> implements IFabricanteDAO{

	@Override
	public void cadastrar(Fabricante entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
			this.entityManager.persist(entidade);
			this.entityManager.getTransaction().commit();
	}

	@Override
	public void alterar(Fabricante entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
			this.entityManager.persist(entidade);
			this.entityManager.getTransaction().commit();
	}

	@Override
	public void remover(Fabricante entidade) {
			if (!this.entityManager.getTransaction().isActive());
			this.entityManager.getTransaction().begin();
			this.entityManager.remove(entidade);
			this.entityManager.getTransaction().commit();	
	}

	@Override
	public Fabricante pesquisarID(long id) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
		Fabricante f = this.entityManager.find(Fabricante.class, id); 
		return f;
	}

	@Override
	public List<Fabricante> pesquisarTodos() {
		if(!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		TypedQuery<Fabricante> q = this.entityManager.createQuery("SELECT f FROM Fabricante f", Fabricante.class);
		List<Fabricante> fabricantes = q.getResultList();
		return fabricantes;
	}

}
